package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.product.api.domain.Brand;
import com.spzx.product.domain.CategoryBrand;
import com.spzx.product.mapper.CategoryBrandMapper;
import com.spzx.product.service.CategoryBrandService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 分类品牌 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2024-10-26
 */
@Service
public class CategoryBrandServiceImpl extends ServiceImpl<CategoryBrandMapper, CategoryBrand> implements CategoryBrandService {

    @Autowired
    private CategoryService categoryService;

    @Override
    public List<CategoryBrand> getPageList(CategoryBrand categoryBrand) {
        return baseMapper.getPageList(categoryBrand);
    }

    @Override
    public boolean saveCategoryBrand(CategoryBrand categoryBrand) {
        Long aLong = baseMapper.selectCount(new LambdaQueryWrapper<CategoryBrand>().eq(CategoryBrand::getBrandId, categoryBrand.getBrandId())
                .eq(CategoryBrand::getCategoryId, categoryBrand.getCategoryId()));
        if (aLong > 0) throw new ServiceException("请求添加已存在");
        return aLong > 0;
    }

    @Override
    public CategoryBrand selectCategoryBrandById(Long id) {
        CategoryBrand categoryBrand = baseMapper.selectById(id);
        List<Long> categoryList = categoryService.getCategoryList(categoryBrand.getCategoryId());
        categoryBrand.setCategoryList(categoryList);
        return categoryBrand;
    }

    @Override
    public int updateCategoryBrandById(CategoryBrand categoryBrand) {
        //根据id查询数据库中的分类品牌数据
        CategoryBrand categoryBrandDatabase = baseMapper.selectById(categoryBrand.getBrandId());
        //判断用户是否更新
        if(categoryBrand.getBrandId().equals(categoryBrandDatabase.getBrandId()) && categoryBrand.getCategoryId().equals(categoryBrandDatabase.getCategoryId())){
            //当前分类品牌并没有修改
            return 1;

        }
        //判断当前修改之后的分类品牌数据库是否存在
        //根据分类ID和品牌ID查询
        LambdaQueryWrapper<CategoryBrand> categoryBrandLambdaQueryWrapper = new LambdaQueryWrapper<>();
        categoryBrandLambdaQueryWrapper.eq(CategoryBrand::getCategoryId,categoryBrand.getCategoryId())
                .eq(CategoryBrand::getBrandId,categoryBrand.getBrandId());
        //调用带条件查询的方法
        CategoryBrand categoryBrand1 = baseMapper.selectOne(categoryBrandLambdaQueryWrapper);
        if(categoryBrand1 != null)
            throw new ServiceException("该分类品牌数据库中已存在");
        //调用自带的根据id更新方法
        int update = baseMapper.updateById(categoryBrand);
        return update;
    }

    @Override
    public List<Brand> getBrandListByCategoryId(Long id) {
        return baseMapper.getBrandListByCategoryId(id);
    }


}
